// 1. 导入mongoose
const mongoose = require('mongoose');
// 2. 连接数据库
mongoose.connect('mongodb://127.0.0.1/0907');
// 3. 创建连接回调 connection.on("open",callback)
mongoose.connection.on('open',()=>{
    console.log("连接成功");

    // 5. 创建文档结构 new mongoose.Schema()
    const userSchema = new mongoose.Schema({
        name:String,
        age:Number,
        score:Number
    })
    // 6. 创建模型对象 mongoose.model()
    const userModel = mongoose.model("users",userSchema)

    // userModel.create({name:"aaa",age:18},()=>{
    //     console.log("插入成功");
    // });


    // 7. 单条删除 model.deleteOne({},callback)
    // 参数1：查询条件
    // 参数2：回调函数
        // 参数1：错误对象
        // 参数2：返回数据
    // userModel.deleteOne({name:"xiaoli"},(err,data)=>{
    //     if(err) throw err;
    //     console.log(data); //{ acknowledged: true, deletedCount: 1 }
    // })

    // 8. 批量删除 model.deleteMany({}.callback)
    userModel.deleteMany({score:60},(err,data)=>{
        if(err) throw err;
        console.log(data); //{ acknowledged: true, deletedCount: 2 }
    })
})

   